import {
  Login,
  logout,
  GetInfo,
  GetMenuList
} from '@/message/login/login'
import {
  getToken,
  setToken,
  removeToken,
  getUserNum,
  setUserNum,
  clearUserNum,
  getUserName,
  setUserName,
  getBranchNum,
  setBranchNum,
  getBranchName,
  setBranchName,
  getLegalPersonNum,
  setLegalPersonNum,
  getLegalPersonName,
  setLegalPersonName,
  setMenu,
  // getBranchNo, setBranchNo,
  // getButton, setButton,
  getIsFirstLoginFLag,
  setIsFirstLoginFLag
  // getAppNum
} from '@/common/utils/auth'
// import md5 from 'js-md5';
import request from '@/common/utils/request'
import generateMenu from '@/common/utils/generateMenu'
// import loginUser from '@/message/login'
// , setIsFirstLoginFLag,
const user = {
  state: {
    token: getToken(),
    UserNum: getUserNum(),
    UserName: getUserName(),
    BranchNum: getBranchNum(),
    BranchName: getBranchName(),
    LegalPersonNum: getLegalPersonNum(),
    LegalPersonName: getLegalPersonName(),
    menu: [],
    // branchNo: getBranchNo(),
    // button: getButton(),
    isFirstLoginFLag: getIsFirstLoginFLag()
    // appNum: getAppNum()
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_USERNUM: (state, UserNum) => {
      state.UserNum = UserNum
    },
    SET_USERNAME: (state, UserName) => {
      state.UserName = UserName
    },
    SET_BRANCHNUM: (state, BranchNum) => {
      state.BranchNum = BranchNum
    },
    SET_BRANCHNAME: (state, BranchName) => {
      state.BranchName = BranchName
    },
    SET_LEGALPERSONNUM: (state, LegalPersonNum) => {
      state.LegalPersonNum = LegalPersonNum
    },
    SET_LEGALPERSONNAME: (state, LegalPersonName) => {
      state.LegalPersonName = LegalPersonName
    },

    // SET_USERNO: (state, userNo) => {
    //   state.userNo = userNo
    // },
    // SET_BRANCHID: (state, branchId) => {
    //   state.branchId = branchId
    // },
    // SET_BRANCHNO: (state, branchNo) => {
    //   state.branchNo = branchNo
    // },
    SET_MENU: (state, menu) => {
      state.menu = menu
    },
    // SET_BUTTON: (state, button) => {
    //   state.button = button
    // },
    SET_ISFIRSTLOGINFLAG: (state, isFirstLoginFLag) => {
      state.isFirstLoginFLag = isFirstLoginFLag
    }
    // SET_APPNUM: (state, appNum) => {
    //   state.appNum = appNum
    // }
  },

  actions: {
    // 登录
    Login({
      commit
    }, userInfo) {
      const userNo = userInfo.username.trim()
      // const loginPassword = md5(userInfo.password).toUpperCase()
      const loginPassword = userInfo.password1
      return new Promise((resolve, reject) => {
        const resBody = new Login()
        resBody.data.UserNum = userNo
        resBody.data.AuthData = loginPassword
        resBody.data.BranchNum = userInfo.BranchNum
        request(resBody)
          .then(response => {
            if (response.APP_HEAD.ReturnCode === '000000' && response.RspInfo.PwdStatus !== '1') {
              const data = response.RspInfo
              console.log(data)
              setToken(response.APP_HEAD.aumsToken)
              setUserNum(data.UserInfo.UserNum)
              setUserName(data.UserInfo.UserName)
              setBranchNum(data.BranchInfo.BranchNum)
              setBranchName(data.BranchInfo.BranchName)
              setLegalPersonNum(data.BranchInfo.LegalPersonNum)
              setLegalPersonName(data.BranchInfo.LegalPersonName)
              setIsFirstLoginFLag(data.PwdStatus)
              commit('SET_TOKEN', response.APP_HEAD.aumsToken)
              commit('SET_USERNUM', data.UserInfo.UserNum)
              commit('SET_USERNAME', data.UserInfo.UserName)
              commit('SET_BRANCHNUM', data.BranchInfo.BranchNum)
              commit('SET_BRANCHNAME', data.BranchInfo.BranchName)
              commit('SET_LEGALPERSONNUM', data.BranchInfo.LegalPersonNum)
              commit('SET_LEGALPERSONNAME', data.BranchInfo.LegalPersonName)
              commit('SET_ISFIRSTLOGINFLAG', data.PwdStatus)
              resolve(response)
            } else {
              resolve(response)
            }
          })
          .catch(error => {
            reject(error)
            console.log(error)
          });
      })
    },
    Login1({
      commit
    }, userInfo) {
      setUserNum(userInfo.userid)
      setBranchNum(userInfo.branchnum)
      commit('SET_USERNUM', userInfo.userid)
      commit('SET_BRANCHNUM', userInfo.branchnum)
      const userNo = userInfo.userid
      // const loginPassword = md5(userInfo.password).toUpperCase()
      // const loginPassword = userInfo.password1
      return new Promise((resolve, reject) => {
        const resBody = new Login()
        resBody.data.UserNum = userNo
        resBody.data.LoginMode = '0' // 指纹登录
        resBody.data.AuthFlag = '1' // 不认证
        // resBody.data.AuthData = '6A18A81B906F22F3'
        resBody.data.BranchNum = userInfo.branchnum
        request(resBody)
          .then(response => {
            if (response.APP_HEAD.ReturnCode === '000000' && response.RspInfo.PwdStatus !== '1') {
              const data = response.RspInfo
              console.log(data)
              setToken(response.APP_HEAD.aumsToken)
              setUserNum(data.UserInfo.UserNum)
              setUserName(data.UserInfo.UserName)
              setBranchNum(data.BranchInfo.BranchNum)
              setBranchName(data.BranchInfo.BranchName)
              setLegalPersonNum(data.BranchInfo.LegalPersonNum)
              setLegalPersonName(data.BranchInfo.LegalPersonName)
              setIsFirstLoginFLag(data.PwdStatus)
              commit('SET_TOKEN', response.APP_HEAD.aumsToken)
              commit('SET_USERNUM', data.UserInfo.UserNum)
              commit('SET_USERNAME', data.UserInfo.UserName)
              commit('SET_BRANCHNUM', data.BranchInfo.BranchNum)
              commit('SET_BRANCHNAME', data.BranchInfo.BranchName)
              commit('SET_LEGALPERSONNUM', data.BranchInfo.LegalPersonNum)
              commit('SET_LEGALPERSONNAME', data.BranchInfo.LegalPersonName)
              commit('SET_ISFIRSTLOGINFLAG', data.PwdStatus)
              resolve(response)
            } else {
              resolve(response)
            }
          })
          .catch(error => {
            reject(error)
            console.log(error)
          });
      })
    },
    // 获取用户信息
    GetInfo({
      commit,
      state
    }) {
      return new Promise((resolve, reject) => {
        const resBody = new GetInfo()
        resBody.data.userId = state.userId
        request(resBody).then(response => {
          if (response.SYS_HEAD.ReturnCode === '000000') {
            const data = response.RSP_BODY
            // if (data.menuElementKeyList && data.menuElementKeyList.length > 0) { // 验证返回的roles是否是一个非空数组
            //   commit('SET_ROLES', data.menuElementKeyList)
            // } else {
            //   reject('getInfo: roles must be a non-null array !')
            // }
            // setBranchId(data.branchId)
            // setBranchName(data.branchName)
            // setBranchNo(data.branchNo)
            // setUserName(data.userName)
            setMenu(data.menuElementKeyList)
            // setButton(data.buttonElementKeyList)
            // setUserNo(data.userNo)
            // setIsFirstLoginFLag(data.isFirstLoginFLag)
            // commit('SET_BRANCHID', data.branchId)
            // commit('SET_BRANCHNAME', data.branchName)
            // commit('SET_BRANCHNO', data.branchNo)
            // commit('SET_USERNAME', data.userName)
            // commit('SET_USERNO', data.userNo)
            commit('SET_MENU', data.menuElementKeyList)
            // 提前处理一下
            // var buttonTempArr=[];
            // data.buttonElementKeyList.map(function(item){buttonTempArr[item]=1});
            // commit('SET_BUTTON', buttonTempArr)
            // commit('SET_BUTTON', data.buttonElementKeyList)
            // commit('SET_ISFIRSTLOGINFLAG', data.isFirstLoginFLag)
            resolve(response)
          } else {
            resolve(response)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取菜单
    GetMenu({
      commit
    }) {
      return new Promise((resolve, reject) => {
        const resBody = new GetMenuList()
        request(resBody).then(response => {
          if (response.APP_HEAD.ReturnCode === '000000') {
            const tradeList = response.RspInfo.TradeMenuList || []
            const dirList = response.RspInfo.DirMenuList || []
            let menuArr = [];
            menuArr = generateMenu.generateMenu(dirList, tradeList);
            console.log(menuArr)
            setMenu(menuArr)
            commit('SET_MENU', menuArr)
            resolve(response)
          } else {
            resolve(response)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    LogOut({
      commit,
      state
    }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({
      commit
    }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        commit('SET_MENU', [])
        commit('SET_USERNUM', '')
        // commit('SET_BUTTON', [])
        removeToken()
        clearUserNum()
        resolve()
      })
    },

    // sso
    SSOLogin({
      commit
    }, view) {
      commit('SET_USERNUM', view.userid)
      commit('SET_TOKEN', view.aumsToken)
    }
  }
}

export default user
